package com.kotlin.book.adapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.book.R
import com.kotlin.book.api.CommentApi
import com.kotlin.book.databinding.ItemReadCommentBinding
import com.kotlin.book.entry.Comment
import com.kotlin.book.util.Util

class ReadCommentItemAdapter(
    private var itemList: List<Comment>
) : RecyclerView.Adapter<ReadCommentItemAdapter.ViewHolder>() {
    var page = 1

    class ViewHolder(itemBinding: ItemReadCommentBinding) :
        RecyclerView.ViewHolder(itemBinding.root) {
        val commentNickname = itemBinding.commentNickname
        val commentTime = itemBinding.commentTime
        val commentAvatar = itemBinding.commentAvatar
        val commentContent = itemBinding.commentContent
        val review = itemBinding.review
        val praise = itemBinding.praise
        val iconPraise = itemBinding.iconPraise
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding =
            ItemReadCommentBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return itemList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = itemList[position]
        holder.commentNickname.text = item.userNickname
        holder.commentTime.text = item.createTime?.substring(0, 10)
        holder.commentContent.text = item.content
        holder.review.text = item.replyCount.toString()
        holder.praise.text = item.praiseCount.toString()
        if (item.isPraise) {
            holder.iconPraise.setImageResource(R.drawable.icon_praise_active)
            holder.praise.setTextColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.text_praise_color
                )
            )
        }
        holder.iconPraise.setOnClickListener {
            CommentApi.updatePraiseUser(mapOf("commentId" to item.id)) { response ->
                response?.data?.let { data ->
                    (holder.itemView.context as? Activity)?.runOnUiThread {
                        itemList[holder.adapterPosition].isPraise = data.isPraise
                        notifyItemChanged(holder.adapterPosition)
                    }
                }
            }
        }
        Util.getImage(item.userAvatar, holder.commentAvatar, holder.itemView.context)
    }

    fun pushData(data: List<Comment>) {
        val mutableList = itemList.toMutableList()
        val index = itemList.size
        page++
        mutableList.addAll(data)
        itemList = mutableList.toList()
        notifyItemRangeInserted(index, data.size)
    }

    fun addComment(data: Comment) {
        val mutableList = itemList.toMutableList()
        mutableList.add(0, data)
        itemList = mutableList.toList()
        notifyItemInserted(0)
    }
}